package com.hyk.commonLib.common.dialogView;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.databinding.Bindable;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.haibin.calendarview.LunarCalendar;
import com.hyk.commonLib.common.dialogView.base.BottomPopupViewExt;
import com.hyk.commonLib.common.utils.TimeUtils;
import com.hyk.commonLib.common.utils.eventHandler.ViewEventHandler;
import com.hyk.commonLib.common.utils.viewModel.BaseObservableModel;
import com.hyk.commonLib.databinding.CommonLibDialogViewBottomCalendarBinding;

import org.joda.time.DateTime;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

public class BottomCalendarDialog extends BottomPopupViewExt<CommonLibDialogViewBottomCalendarBinding> {
    public BottomCalendarDialog(@NonNull Context context) {
        super(context);
        BottomCalendarDialogModel model = new BottomCalendarDialogModel();
        BottomCalendarDialogEventHandler handler = new BottomCalendarDialogEventHandler(this, model);

        getDataBinding().setModel(model);
        getDataBinding().setHandler(handler);

        dataBinding.calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                if (!isClick) {
                    getDataBinding().getModel().setSelectDate(TimeUtils.formatMillis(calendar.getTimeInMillis()));
                    return;
                }
                if (getDataBinding().getModel().onDateSelectListener != null) {
                    getDataBinding().getModel().onDateSelectListener.onSelect(TimeUtils.formatMillis(calendar.getTimeInMillis(), getDataBinding().getModel().dateFormatter), calendar.getYear(), calendar.getMonth(), calendar.getDay());
                }
                dismiss();
            }
        });
    }

    @Getter(onMethod_ = {@Bindable})
    @Setter(onMethod_ = {@BaseObservableModel.PropertiesChanged(value = true)})
    @Accessors(chain = true)
    public static class BottomCalendarDialogModel extends BaseObservableModel {
        @Getter(AccessLevel.NONE)
        @Setter(onMethod_ = {@BaseObservableModel.PropertiesChanged(value = false, names = {"selectYear", "selectMonthOfYear", "selectDayOfMoth", "lunar"})})
        private String selectDate = TimeUtils.format();
        @Setter
        private OnDateSelectListener onDateSelectListener;
        @Setter
        private String dateFormatter = TimeUtils.TIME_FORMATTER_DATE_ONLY;

        @Bindable
        public String getLunar() {
            return LunarCalendar.getLunarText(getSelectYear(), getSelectMonthOfYear(), getSelectDayOfMoth());
        }

        @Bindable
        public Integer getSelectYear() {
            return Integer.valueOf(TimeUtils.convert(selectDate, "YYYY"));
        }

        @Bindable
        public Integer getSelectMonthOfYear() {
            return Integer.valueOf(TimeUtils.convert(selectDate, "MM"));
        }

        @Bindable
        public Integer getSelectDayOfMoth() {
            return Integer.valueOf(TimeUtils.convert(selectDate, "dd"));
        }

        public int getTodayDayOfMonth() {
            return DateTime.now().getDayOfMonth();
        }

    }

    public static class BottomCalendarDialogEventHandler extends ViewEventHandler<BottomCalendarDialog, BottomCalendarDialogModel> {

        public BottomCalendarDialogEventHandler(@NonNull BottomCalendarDialog view, BottomCalendarDialogModel model) {
            super(view, model);
        }

        public void scrollToToday() {
            getContainer().getDataBinding().calendarView.scrollToCurrent();
        }
    }

    public interface OnDateSelectListener {
        void onSelect(String date, int year, int monthOfYear, int dayOfMonth);
    }
}
